package com.logic.landseaserver.models.payment.channel;

import java.util.Map;

import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.domain.BillPayment;
import com.logic.landseaserver.models.payment.BillPaymentCollections;
import com.logic.landseaserver.models.payment.PaymentConstant;
import com.logic.landseaserver.models.payment.bean.PaymentHelperBean;
import com.logic.landseaserver.models.payment.bean.PaymentQueryRequest;
import com.logic.landseaserver.models.payment.bean.PaymentQueryResult;
import com.logic.landseaserver.models.payment.bean.PaymentRequest;

/**
 * 
 *
 * 项目名称：system-server</br>
 * 类名称：AbstractPaymentChannel</br>
 * 类描述：支付渠道</br>
 * 创建人：Aaron</br>
 * 创建时间：2017年7月17日 下午11:05:31</br>
 * @version 1.0
 *
 */
public abstract class AbstractPaymentChannel
{
    /**
     * 
     * [简要描述]：获取支付日志打印前缀</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-07-12)</br>
     *
     * @return log info
     *
     */
    protected String getLog(BillPayment payment)
    {
        String billTypeName = BillPaymentCollections.getBillTypeName(payment.getBillType());
        String payType = payment.getPayType();
        String messageId = getMessageId(payType, String.valueOf(payment.getBillId()), payment.getId());
        
        // 规则：账单类型|支付方式|处理类名称|消息Id|
        StringBuilder logStringBuilder = new StringBuilder();
        logStringBuilder.append(billTypeName);
        logStringBuilder.append(payType);
        logStringBuilder.append("|");
        logStringBuilder.append(getProcessClassName());
        logStringBuilder.append("|");
        logStringBuilder.append(messageId);
        logStringBuilder.append("|");
        
        return logStringBuilder.toString();
    }
    
    /**
     * 
     * [简要描述]：</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-07-12)</br>
     *
     * @param payType 支付类型
     * @param billId 账单id
     * @param paymentId 支付id
     * @return 消息id
     *
     */
    protected String getMessageId(String payType, String billId, Integer paymentId)
    {
        // 作用：根据messageId可以快速的跟踪一次支付从开始到回调的全过程，方便定位问题
        
        // 如果还未生成paymentId
        if (null == paymentId)
        {
            // 规则：payType_billId
            return payType + PaymentConstant.UNDERLINE_SEPARATOR + billId;
        }
        
        // 规则: payType_billId_paymentId
        return payType + PaymentConstant.UNDERLINE_SEPARATOR + billId + PaymentConstant.UNDERLINE_SEPARATOR + paymentId;
    }
    
    /**
     * 
     * [简要描述]：</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-07-17)</br>
     *
     * @param paymentRequest 支付请求对象
     * @return BillPayment 入库需要用的对象
     *
     */
    public abstract BillPayment buildBillPayment(PaymentRequest paymentRequest) throws LandseaException;
    
    /**
     * 
     * [简要描述]：</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-07-17)</br>
     *
     * @param paymentRequest 支付请求对象
     * @return BillPayment 入库需要用的对象
     *
     */
    public abstract void sendPayRequest(BillPayment billPayment, PaymentHelperBean helperObject, Map<String, Object> resultMap) throws LandseaException;
    
    /**
     * 
     * [简要描述]：分析结果，将结果赋值到billPayment对象</br>
     * [详细描述]：根据不同的支付渠道，做不同的解析</br>
     * [作者]：Aaron(2017-07-19)</br>
     *
     * @param billPayment 支付记录对象
     * @param paymentResponse 通知结果对象
     * @param helperObject 帮助对象
     * @param resultMap 返回的结果集
     * @return true继续执行后续操作  false结束流程返回结果
     * 
     * @throws LandseaException
     *
     */
    public abstract boolean analysisResult(BillPayment billPayment, Object paymentResponse, PaymentHelperBean helperObject, Map<String, Object> resultMap) throws LandseaException;
    
    /**
     * 
     * [简要描述]：查询第三方订单</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-07-25)</br>
     *
     * @param paymentQuery 查询订单接口的请求
     * @param billPayment 支付记录
     * @param paymentQueryResult 查询第三方接口的响应
     * @param resultMap 返回到页面的数据
     * @throws LandseaException
     *
     */
    public abstract void paymentQuery(PaymentQueryRequest paymentQuery, BillPayment billPayment, PaymentQueryResult paymentQueryResult, Map<String, Object> resultMap) throws LandseaException;
    
    /**
     * 
     * [简要描述]：回调处理完毕封装响应结果</br>
     * [详细描述]：根据不同的支付渠道，做不同的返回值</br>
     * [作者]：Aaron(2017-07-19)</br>
     *
     * @param billPayment 支付记录对象
     * @param helperObject 帮助对象
     * @param resultMap 结果集
     * @throws LandseaException
     *
     */
    public abstract void buildResponseResult(BillPayment billPayment, PaymentHelperBean helperObject, Map<String, Object> resultMap) throws LandseaException;
    
    /**
     * 
     * [简要描述]：获取处理类</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-07-12)</br>
     *
     * @return
     *
     */
    protected abstract String getProcessClassName();
}
